package com.landscope.issue.render;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Result {

	private int code = 0;
	private String message = "";

	private Map<String, Object> map = new HashMap<>();

	public static Result error() {
		return new Result().code(-1).message("操作失败");
	}

	public static Result success() {
		return new Result().code(0).message("操作成功");
	}

	public static Result error(String str) {
		return new Result().code(-1).message(str);
	}
	public static Result login(String msg) {
		if(StrUtil.isBlank(msg)) {
			return new Result().code(1001).message("请重新授权");
		}
		return new Result().code(1001).message(msg);
	}

	public static Result success(String str) {
		return new Result().code(0).message(str);
	}

	public Boolean isSuccess(){
		if(code == 0)
			return true;
		return false;
	}

	public Object getValue(String key){
		return map.get(key);
	}

	public Map<String, Object> render() {
		Map<String, Object> map = new HashMap<>();
		map.put("code", code);
		map.put("message", message);

		for (String key : this.map.keySet()) {
			map.put(key, this.map.get(key));
		}
//		if(map.toString().length() > 1000)
//			System.out.println("返回参数过长");
//		else
			System.out.println("返回参数--------"+map);
		return map;
	}
	public JSONObject renderJson() {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", code);
		jsonObject.put("message", message);
		for (String key : this.map.keySet()) {
			jsonObject.put(key, this.map.get(key));
		}
		return jsonObject;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Result code(int code) {
		this.code = code;
		return this;
	}

	public Result message(String message) {
		this.message = message;
		return this;
	}

	public Result value(String key, Object value) {
		map.put(key, value);
		return this;
	}

}
